package controllers;

import java.util.ArrayList;
import java.util.List;

import play.data.validation.Required;
import play.mvc.Controller;
import models.Organization;
import models.RelationNames;

public class RelationName extends Controller {
	/**
	 * @author Menna Magdy
	 * 
	 *         save in it the organizationId that we stand on
	 */
	public static Long OrganizationId1;
	/**
	 * @author Menna Magdy save in it the user's permission type
	 */
	public static String permissionType;
	/**
	 * renders the page relationNamesHome to show the application for the
	 * creating new relation name
	 * 
	 * @param id
	 *            the id of the organization
	 * @author Menna Magdy
	 */
	public static void relationNamesHome(Long idForOrganization,String userType) {
		try{
		OrganizationId1 = idForOrganization;
		permissionType=userType;
		render(idForOrganization,userType);
		}
		catch(NullPointerException error){
			Requests.contentNotFound();
		}
	}

	/**
	 * validate if the new relation name was valid or not then if it walide it
	 * will be added to the relationNames List in the organization ,then renders
	 * a page to confirm the creator that the relation name was added
	 * successfully and if the request was
     *
	 * @param createName
	 *            the new relation name entered
	 * @author Menna Magdy
	 */
	public static void addNameForRelation(@Required String createName) {
		try{
		Organization organization = Organization.findById(OrganizationId1);
		String userType=permissionType;
		if (validation.hasErrors()) {
			flash.error("Please enter the relation name!");
			relationNamesHome(OrganizationId1,userType);
		}
		boolean action = organization.addRelationName(createName);
		if (action == false) {
			flash.error("This relation name is already existed!!");
			relationNamesHome(OrganizationId1,userType);
		}
		List<RelationNames> view = organization.relationNameList;
		String message="Your new relation has been succefully added!!";
		Relations.showNames(message);
		}
		catch(NullPointerException error){
			Requests.contentNotFound();
		}
	}
	
}
